<?php


	class DBHelper{
		
		private $_Query;
		private $_Result;
		private $_NumRows;
		private $_LastIdInserted; 

		public function getResult(){
			return $this->_Result;
		}
		
		public function setResult($value){
			$this->_Result = $value;
		}
		
		public function getNumRows(){
			return $this->_NumRows;
		}
		
		public function setNumRows($value){
			$this->_NumRows = $value;
		}
		
		public function getQuery(){
			return $this->_Query;
		}
		
		public function setQuery($value){
			$this->_Query = $value;
		}
		
		public function getLastIdInserted(){
			return $this->_LastIdInserted;
		}
		
		public function setLastIdInserted($value){
			$this->_LastIdInserted = $value;
		}
		
		function __construct($query){
			$this->setQuery(trim($query));
			$query = explode (' ', $this->getQuery());
			switch ($query[0]){
				case 'SELECT':
					$this->_select();
				break;
				default:
					$this->_executeQuery();
				break;
			}
		}
		
		private function _connect(){
			$conn = @mysql_connect(HOSTNAME . PORT, USERNAME, PASSWORD);
			if (!$conn)
				die('Could not connect.');
		}

		private function _select($arrParameters = null){
			$return = array();
			
			$this->_connectAndSelectDB();
			$result = mysql_query($this->getQuery());
			while ($row = mysql_fetch_assoc($result))
				$return[] = $row;
			$this->setNumRows(mysql_num_rows($result));
			$this->setResult($return);
			$this->_close();
		}

		public function _executeQuery(){
			$this->_connectAndSelectDB();
			$return = mysql_query($this->getQuery());
			$this->setNumRows(mysql_affected_rows());
			$this->setLastIdInserted(mysql_insert_id());
			$this->_close();
			return $return;
		}

		private function _connectAndSelectDB(){
			$this->_connect();
			$this->_selectDataBase();			
		}

		private function _selectDataBase(){
			if (!mysql_select_db(DBNAME))
				die('Invalid data base');
		}
		
		private function _close(){
			mysql_close();
		}
	}
?>